package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.ICourseDao;
import com.school.base.model.Course;
import com.school.common.tags.PageBean;

/**
 * @author gaobiao
 * 
 */
public class CourseService {
	private static Logger logger = Logger.getLogger(CourseService.class);
	private ICourseDao courseDao;

	public ICourseDao getCourseDao() {
		return courseDao;
	}

	public void setCourseDao(ICourseDao courseDao) {
		this.courseDao = courseDao;
	}

	public Course getCourseById(int id) {
		return courseDao.getCourseById(id);
	}

	public List<Course> getCourseList(Course course, PageBean pageBean) {
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(courseDao.getTotalCount(course));
		return courseDao.getCourseList(course, pageBean.getOffset(), pageBean.getPageSize());
	}

	public void updateCourse(Course course) throws Exception {
		if (course != null) {
			if (course.getName() == null || course.getName().isEmpty()) {
				logger.error("update course fail:courseName is empty");
				throw new Exception("课程名称不能为空");
			}
			if (course.getClassId() <= 0) {
				logger.error("update course fail:classId is empty");
				throw new Exception("所属班级不能为空");
			}
			if (course.getTeacherId() <= 0) {
				logger.error("update course fail:teacherId is empty");
				throw new Exception("授课教师不能空");
			}
			if (course.getSubjectId() <= 0) {
				logger.error("update course fail:subjectId is empty");
				throw new Exception("科目不能为空");
			}
			courseDao.updateCourse(course);
		} else {
			throw new Exception("course对象不能为空");
		}
		courseDao.updateCourse(course);
	}

	public void deleteCourse(int id) {
		courseDao.deleteCourse(id);
	}

	public void insertCourse(Course course, Integer creatorId) throws Exception {
		if (course != null) {
			if (course.getName() == null || course.getName().isEmpty()) {
				logger.error("insert course fail:courseName is empty");
				throw new Exception("课程名称不能为空");
			}
			if (course.getClassId() <= 0) {
				logger.error("insert course fail:classId is empty");
				throw new Exception("所属班级不能为空");
			}
			if (course.getTeacherId() <= 0) {
				logger.error("insert course fail:teacherId is empty");
				throw new Exception("授课教师不能空");
			}
			if (course.getSubjectId() <= 0) {
				logger.error("insert course fail:subjectId is empty");
				throw new Exception("科目不能为空");
			}
		} else {
			throw new Exception("course对象不能为空");
		}
		course.setCreatorId(creatorId);
		courseDao.insertCourse(course);

	}
}
